package com.example.milimili;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.milimili.bean.NewsInfo;
import com.example.milimili.util.GlideImageLoader;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewsActivity extends AppCompatActivity {

    private ImageButton backBtn,collectBtn;
    private TextView titleText,contentText,publisherText,publishTimeText;
    private Map<String,Object> dataMap;
    private String path;
    private int position;
    public static boolean flag;
    private Banner banner;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        initView();
        Bundle bundle=getIntent().getExtras();
        SerializableMap sMap= (SerializableMap) bundle.getSerializable("map");
        dataMap=sMap.getMap();
        path=sMap.getPath();
//        Log.e("collect:",path);
        position=sMap.getPosition();
        String imageAll=dataMap.get("image").toString();
        String[] imageArray=imageAll.split(", ");
        List<String> images = new ArrayList<>();
        for(int i=0;i<imageArray.length;i++)
        {
            images.add(imageArray[i]);
//            Log.e("image:",imageArray[i]);
        }
        banner = findViewById(R.id.imageBanner);
        banner.setImageLoader(new GlideImageLoader());   //设置图片加载器
        banner.setImages(images);  //设置banner中显示图片
        banner.start();  //设置完毕后调用
        titleText.setText(dataMap.get("title").toString());
        contentText.setText(dataMap.get("content").toString());
        if(dataMap.get("publisher")!=null)
        {
            publisherText.setText(dataMap.get("publisher").toString());
        }
        if(dataMap.get("publishTime")!=null)
        {
            publishTimeText.setText(dataMap.get("publishTime").toString());
        }
        contentText.setMovementMethod(ScrollingMovementMethod.getInstance());
        setHistory();
        MySQLiteOpenHelper mySQLiteOpenHelper=new MySQLiteOpenHelper(NewsActivity.this);
        if(!mySQLiteOpenHelper.checkFromDbByTitle(1,dataMap.get("title").toString()))
        {
            collectBtn.setImageDrawable(getResources().getDrawable(R.drawable.ic_star_24));
        }
    }

    private void setHistory() {
        MySQLiteOpenHelper mySQLiteOpenHelper=new MySQLiteOpenHelper(NewsActivity.this);
        NewsInfo newsInfo = new NewsInfo();
        newsInfo.setTitle(dataMap.get("title").toString());
        newsInfo.setImage(dataMap.get("image").toString());
        newsInfo.setPath(path);
        newsInfo.setPosition(position);
        newsInfo.setContent(dataMap.get("content").toString());
        long rowId = mySQLiteOpenHelper.insertData(2,newsInfo);
    }

    private void initView() {
        backBtn=findViewById(R.id.backButton);
        collectBtn=findViewById(R.id.collectButton);
        titleText=findViewById(R.id.title);
        contentText=findViewById(R.id.content);
        publisherText=findViewById(R.id.publisher);
        publishTimeText=findViewById(R.id.publishTime);
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    NewsActivity.this.finish();
            }
        });
        collectBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MySQLiteOpenHelper mySQLiteOpenHelper=new MySQLiteOpenHelper(NewsActivity.this);
                NewsInfo newsInfo = new NewsInfo();
                newsInfo.setTitle(dataMap.get("title").toString());
                newsInfo.setImage(dataMap.get("image").toString());
                newsInfo.setPath(path);
                newsInfo.setContent(dataMap.get("content").toString());
                newsInfo.setPosition(position);
                collectBtn.setImageDrawable(getResources().getDrawable(R.drawable.ic_star_24));
                long rowId = mySQLiteOpenHelper.insertData(1,newsInfo);
                if(rowId!=-1)
                {
                    Toast.makeText(NewsActivity.this,"收藏成功",Toast.LENGTH_LONG);
                }
                else
                {
                    Toast.makeText(NewsActivity.this,"已收藏",Toast.LENGTH_LONG);
                }
            }
        });
    }

}